package com.starpl.mobile.android.monthunsae.november.handler;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.starpl.mobile.android.monthunsae.november.R;
import com.starpl.mobile.android.platform.command.StarplCommand;
import com.starpl.mobile.android.platform.common.image.ImageDownloader;
import com.starpl.mobile.android.platform.common.network.HttpHandler;
import com.starpl.mobile.android.platform.component.BaseListHandler;
import com.starpl.mobile.android.platform.domain.RejectedUser;


/**
 * 쪽지 차단 리스트 핸들러 쪽지 차단된 유저 목록을 가져옵니다. 차단한 유저에 대해 [차단해제]를 할 수 있습니다.
 * 
 * @author 우연화
 * @since 2011.05.03
 */
public class MessageBlockListHandler extends BaseListHandler implements OnClickListener
{
	private static String TAG = "MessageBlockListHandler";
	
	private ArrayList<RejectedUser> mRejectUserList;
	private int mPage;

	/**
	 * 쪽지 차단 리스트 핸들러 생성자
	 * 
	 * @param activity 액티비티
	 * @param userId 유저아이디
	 */
	public MessageBlockListHandler(Activity activity)
	{
		super(activity);
		this.mRejectUserList = new ArrayList<RejectedUser>();
		this.mPage = 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.starpl.platform.component.BaseListHandler#getArrayList()
	 */
	@Override
	public ArrayList<?> getArrayList()
	{
		return mRejectUserList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.starpl.platform.component.BaseListHandler#getRowView(int,
	 * android.view.View)
	 */
	@Override
	public View getRowView(int position, View convertView)
	{
		RejectedUser rejectUser = mRejectUserList.get(position);
		
		convertView = inflate(R.layout.message_block_row);

		ImageView ivThumbnail = (ImageView) convertView.findViewById(R.id.message_block_row_iv_thumbnail);
		TextView tvNickName = (TextView) convertView.findViewById(R.id.message_block_row_tv_nickname);
		TextView tvDateTime = (TextView) convertView.findViewById(R.id.message_block_row_tv_datetime);
		ImageButton ibUnBlock = (ImageButton) convertView.findViewById(R.id.message_block_row_ib_unblock);

		ibUnBlock.setOnClickListener(this);
		
		ImageDownloader.download(rejectUser.getProfileImgPath(), ivThumbnail);
		
		tvNickName.setText(rejectUser.getStarName());
		tvDateTime.setText(rejectUser.getRegDate());

		ibUnBlock.setTag(position);
		convertView.setTag(position);
		
		return convertView;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.starpl.platform.component.BaseListHandler#onRowClick(android.view
	 * .View)
	 */
	@Override
	public void onRowClick(View v)
	{
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.starpl.platform.component.BaseListHandler#onRowClear()
	 */
	@Override
	public void onRowClear()
	{
		mRejectUserList.clear();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.starpl.platform.component.BaseListHandler#getFooterView()
	 */
	@Override
	public View getFooterView()
	{
		return inflate(R.layout.community_footer);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.starpl.platform.component.BaseListHandler#loadContent()
	 */
	@Override
	public void loadContent()
	{
		HttpHandler handler = new HttpHandler(mParentActivity)
		{

			@Override
			public void didSucceed(Object result)
			{
				try
				{
					JSONArray array = ((JSONObject)result).getJSONArray("data");
					
					int length = array.length();
					
					for(int i = 0 ; i < length ; i++)
					{
						RejectedUser reject = new RejectedUser(array.getJSONObject(i));
						mRejectUserList.add(reject);
					}	
					
					if(length == 0)
					{
						alert("쪽지 차단된 유저가 없습니다");
					}
				}
				catch(JSONException e)
				{
					Log.e(TAG, e.getMessage());
				}
				
				
				// 다음 페이지를 검사해 토글할지 푸터뷰를 없앨지 결정한다
				if(mPage > 1)
				{
					toggleLockListView();
				}
				else
				{
					removeFooterView();
				}

				notifyDataSetChanged();
			}

		};
		
		StarplCommand.getRejectedMessageUser(handler);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.starpl.platform.component.BaseListHandler#onInit()
	 */
	@Override
	public void onInit()
	{
		showFooterView();

		setAdapter();

		loadContent();
	}

//	private void alert
	
	
	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v)
	{
		switch(v.getId())
		{
		
		// 차단해제 버튼 클릭
		case R.id.message_block_row_ib_unblock:
			HttpHandler unblockHandler = new HttpHandler(mParentActivity)
			{
				@Override
				public void didSucceed(Object result)
				{
					refresh();
				}
			};
			
			RejectedUser rejectUser = mRejectUserList.get((Integer)v.getTag());
			StarplCommand.updateRejectedUserStatus(unblockHandler, rejectUser.getUserIdx(), false);
			break;
		
		}
	}

}
